import 'package:flutter/material.dart';
import 'package:flutter_module/beans/data_layer_bean.dart';
import 'package:flutter_module/map/ni_types.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:flutter_module/views/mapFragmentWidgets/map_title_bar.dart';
import 'package:provider/src/provider.dart';

import '../resources_config.dart';
import 'base_page.dart';

class LayerListPage extends BasePage {
  DrawerManagerViewModel? _read;


  @override
  List<Widget> createWidgets(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    List<Widget> widgets = [];
    Map<String, List<DataLayer>> map = {};
    for (var item in _read!.dataLayerList) {
      if (map.containsKey(item.projectId)) {
        map[item.projectId]?.add(item);
      } else {
        List<DataLayer> list = [];
        list.add(item);
        map.putIfAbsent(item.projectId, () => list);
      }
    }

    for (var item in _read!.projectList) {
      List<DataLayer>? layers = map[item.uuid];
      List<Widget> widgetList = [];
      if (layers != null) {
        for (var dataLayer in layers) {
          widgetList.add(
            ListTile(
              title: Text(dataLayer.layerName),
              leading: ResourcesConfig.getGeometryTypeIcon(dataLayer.geometryType),
              trailing: const Icon(Icons.navigate_next_outlined),
              onTap: () {
                _read!.createElementEditPage(dataLayer);
              },
            ),
          );
        }
      }
      widgets.add(
        Card(
          color: Colors.blue,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
          elevation: 8,
          child: ExpansionTile(
            leading: const Icon(Icons.home),
            subtitle: Text(item.describe),
            backgroundColor: Colors.white,
            title: Text(item.name),
            children: widgetList,
          ),
        ),
      );
    }
    return widgets;
  }

  @override
  void dispose() {}

  @override
  MapTitleBarAdapter createMapTitleBarAdapter(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    return MapTitleBarAdapter(
      leading: BackButton(
        color: Colors.black,
        onPressed: () {
          _read!.closeDrawer();
        },
      ),
    );
  }

  @override
  void update() {}
}
